/*
 * @Description: 用户信息 - 接口
 * @Author: DHL
 * @Date: 2022-04-02 14:02:17
 * @LastEditors: DHL
 * @LastEditTime: 2022-11-01 09:52:18
 */

import { $get, $post, $put } from '@/utils/http'
import { ContentTypeEnum } from '@/constant/http'
import {
  UserListResultModel,
  UserModel,
  UserPageParams,
  UserPageResultModel,
  UserParams,
  UserResultModel
} from './model/userModel'
import { Result } from '@/model/baseModel'

/**
 * 分页查询
 */
export const apiUserSearch = (searchParams: UserPageParams) =>
  $get<UserPageResultModel>({
    url: '/sys/twbUser/search',
    params: searchParams
  })

/**
 * 不分页查询
 * @param searchParams
 * @returns
 */
export const searchUsersList = (searchParams: UserParams) =>
  $get<UserListResultModel>({
    url: '/sys/twbUser/searchUsersList',
    params: searchParams
  })

/**
 * 根据ID获取详情
 */
export const apiUserGetById = (id: string) =>
  $get<UserResultModel>({
    url: '/sys/twbUser/getById',
    params: { id: id }
  })

/**
 * 保存信息
 */
export const apiUserSave = (saveParams: UserModel) =>
  $post<UserResultModel>({
    url: '/sys/twbUser/save',
    contentType: ContentTypeEnum.json,
    params: saveParams
  })

/**
 * 更新信息
 */
export const apiUserUpdateById = (updateParams: UserModel) =>
  $put<UserResultModel>({
    url: '/sys/twbUser/updateById',
    contentType: ContentTypeEnum.json,
    params: updateParams
  })

/**
 * 批量删除信息
 */
export const apiUserBatchDeleteByIds = (ids: string) =>
  $post<UserResultModel>({
    url: '/sys/twbUser/batchDeleteByIds',
    params: { ids: ids }
  })

/**
 * 批量逻辑删除
 */
export const apiUserLogicBatchDeleteByIds = (ids: string) =>
  $post<UserResultModel>({
    url: '/sys/twbUser/logicBatchDeleteByIds',
    params: { ids: ids }
  })

/**
 * 修改密码
 * @param passwordParams 修改密码参数
 */
export const apiUserUpdatePassword = (passwordParams: {
  userId: string
  originalPassword: string
  newPassword: string
}) =>
  $post<Result<string>>({
    url: '/sys/auth/updateUserPwd',
    params: passwordParams
  })
export const apiUpdateUserPwdByPhone = (passwordParams: {
  newPassword: string
  confirmPassword: string
  phone: string
  verifyImgCode: string
  verifyPhoneCode: string
}) =>
  $post<Result<string>>({
    url: '/app/auth/updateUserPwdByPhone',
    params: passwordParams
  })

/**
 * 重置密码
 * @param userId
 */
export const apiUserUpdateCurrentPwd = (passwordParams: { userId: string; newPassword: string }) =>
  $post<Result<string>>({
    url: '/sys/twbUser/updateCurrentPwd',
    params: passwordParams
  })

/**
 * 更新用户机构信息
 */
export const apiUserUpdateUserOrgs = (userId: string, orgIds: string) =>
  $post<Result<string>>({
    url: '/sys/twbUser/saveUserOrg',
    params: {
      userId: userId,
      orgIds: orgIds
    }
  })

/**
 * 分页查询角色用户
 * @param searchParams
 */
export const apiUserSearchRoleUsers = (searchParams: UserPageParams) =>
  $get<UserPageResultModel>({
    url: '/sys/twbUser/searchRoleUsers',
    params: searchParams
  })

/**
 * 分页查询角色待选择用户
 * @param searchParams
 */
export const apiSearchRoleSelectUsers = (searchParams: UserPageParams) =>
  $get<UserPageResultModel>({
    url: '/sys/twbUser/searchRoleSelectUsers',
    params: searchParams
  })

export const apiVerifyGetVerifyImgCode = (width: number, height: number) =>
  $post<Result<{ unionCode: string; imgBytes: string }>>({
    url: '/sys/verify/getVerifyImgCode',
    params: {
      width: width,
      height: height
    },
    contentType: ContentTypeEnum.form
  })
/**
 * 获取手机验证码
 */
export const apiVerifyGetPhoneCode = (unionCode: string, verifyImgCode: string, phone: string) =>
  $post<Result<string>>({
    url: '/app/auth/sendSms',
    params: {
      unionCode: unionCode,
      verifyImgCode: verifyImgCode,
      phone: phone
    },
    contentType: ContentTypeEnum.form
  })
